package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @author wz
 * @company 公司
 * @create 2020-04-10 14:32
 */
@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private WxPayFeign wxPayFeign;
    //跳转到微信支付二维码页面
    @GetMapping
    public String wxPay(String orderId, Model model){
        //1.根据OrderId查询订单,如果订单不存在,跳转到错误页面
           Result<Order> orderResult =  orderFeign.findById(orderId);
           if (orderResult.getData() == null){
               return "fail";
           }
        //2.根据订单支付状态进行判断,如果不是未支付的订单,跳转到错误的页面上
        Order order = orderResult.getData();
           if (!"0".equals(order.getPayStatus())){
               return "fail";
           }
        //3.基于payFeign调用统一下单接口,并获取返回结果
           Result payResult =  wxPayFeign.nativePay(orderId,order.getPayMoney());
           if (payResult.getData() == null){
               return "fail";
           }
        //4.封装结果数据
        Map payMap = (Map) payResult.getData();
           payMap.put("payMoney",order.getPayMoney);
           payMap.put("orderId",orderId);
           model.addAllAttributes(payMap);
        return "wxpay";
    }
}
